package view.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;

public class FileChooserButton extends JButton implements ActionListener {

	/**
	 * File Choose Button Control - Will open a dialog box to choose a file
	 */
	private	static final long	serialVersionUID			= 1L;
	public	static final String	SelectedFileChangedProperty	= "SelectedFileChangedProperty";
	
	private JFileChooser				fPanel = null;
	
	public FileChooserButton() {
		this.setText("Open File");
		this.addActionListener(this);
	}
	
	public File	getSelectedFile() {
		try {
			return fPanel.getSelectedFile();
		} catch (Exception e) {
			return null;
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		File	oldFile = getSelectedFile();
		File	newFile = null;
		String	oldName, newName;
		
		if (oldFile == null) 
			oldName = "null";
		else
			oldName = oldFile.getAbsolutePath();
		if (fPanel!=null) {
			fPanel.showDialog(this, "Done");
			newFile = fPanel.getSelectedFile();
		}
		
		if (newFile == null) {
			newName = "null";
		} else {
			newName = newFile.getAbsolutePath();
		}
	
		this.firePropertyChange(SelectedFileChangedProperty, oldName, newName);
	}
	

}
